package com.inspur.dgov.vlbod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inspur.dgov.vlbod.constant.UpAndCollectConstant;
import com.inspur.dgov.vlbod.entity.DemandThumbsUp;
import com.inspur.dgov.vlbod.mapper.DemandThumbsUpMapper;
import com.inspur.dgov.vlbod.service.DemandThumbsUpService;
import com.inspur.dgov.vlbod.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chiyupei
 * @since 2023-05-29
 */
@Slf4j
@Service
public class DemandThumbsUpServiceImpl extends ServiceImpl<DemandThumbsUpMapper, DemandThumbsUp> implements DemandThumbsUpService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 点播点赞
     *
     * @param demandId 点播id
     * @param userId   用户id
     *                 return 是否正常点赞，false为已经点赞
     * @return
     */
    @Override
    public boolean putUp(String demandId, String userId) {
        //构建reidsId
        String redisDemandId = UpAndCollectConstant.DEMAND_THUMBSUP_ZSET + demandId;
        //查看是否点过赞
        Double score = redisTemplate.opsForZSet().score(redisDemandId, userId);
        //redis里面没点过赞
        if (null == score) {
            //获取当前时间的日期
            LocalDateTime currentDateTime = LocalDateTime.now();
            long timeStampFromLocalDateTime = TimeUtils.getTimeStampFromLocalDateTime(currentDateTime);
            //构建点赞对象
            DemandThumbsUp demandThumbsUp = new DemandThumbsUp();
            demandThumbsUp.setDemandId(demandId);
            demandThumbsUp.setUserId(userId);
            demandThumbsUp.setCreateTime(currentDateTime);
            //数据库保存
            boolean save = this.save(demandThumbsUp);
            if (save) {
                log.info("save demandThumbsUp success");
            } else {
                log.error("save demandThumbsUp fail: {}", demandThumbsUp);
            }
            //redis点赞
            redisTemplate.opsForZSet().add(redisDemandId, userId, timeStampFromLocalDateTime);
            return true;
        }
        return false;
    }

    /**
     * 取消点赞
     *
     * @param demandId 点播id
     * @param userId   用户id
     */
    @Override
    public void removeUp(String demandId, String userId) {
        //构建查询对象
        LambdaQueryWrapper<DemandThumbsUp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DemandThumbsUp::getUserId, userId).eq(DemandThumbsUp::getDemandId, demandId);
        //数据库删除
        boolean remove = this.remove(lambdaQueryWrapper);
        if (remove) {
            log.info("remove demandThumbsUp success");
        } else {
            log.error("remove demandThumbsUp fail: demandId:{},userId:{}", demandId, userId);
        }
        String redisDemandId = UpAndCollectConstant.DEMAND_THUMBSUP_ZSET + demandId;
        redisTemplate.opsForZSet().remove(redisDemandId, userId);

    }

    /**
     * 获得点赞总数
     *
     * @param demandId 点播id
     * @return
     */
    @Override
    public long getDemandUpCount(String demandId) {
        String redisDemandId = UpAndCollectConstant.DEMAND_THUMBSUP_ZSET + demandId;
        Long count = redisTemplate.opsForZSet().size(redisDemandId);
        return count != null ? count : 0;
    }

    /**
     * 判断是否点赞过
     *
     * @param demandId 点播id
     * @param userId   用户id
     * @return
     */
    @Override
    public boolean isUp(String demandId, String userId) {
        String redisDemandId = UpAndCollectConstant.DEMAND_THUMBSUP_ZSET + demandId;
        return redisTemplate.opsForZSet().score(redisDemandId, userId) != null;
    }
}
